<?php

namespace frontend\models;

use Yii;
use frontend\models\PnKhoDetail;
/**
 * This is the model class for table "pn_hang_hoa".
 *
 * @property integer $id
 * @property integer $uid
 * @property string $so_ctu 
 * @property string $ma_ctu
 * @property integer $ncc_id
 * @property string $ten_ncc
 * @property string $mst_ncc
 * @property integer $phong_ban_id
 * @property integer $kho_id
 * @property integer $vi_tri_kho_id
 * @property string $dien_giai
 * @property string $ky_hieu_hdon
 * @property string $so_hdon
 * @property integer $ngay_hdon
 * @property integer $pptt_id
 * @property string $tien_hang
 * @property string $tien_thue
 * @property string $tien_thanh_toan
 * @property string $tk_thue
 * @property string $nguoi_nhap
 * @property integer $ngay_nhap
 * @property string $nguoi_sua
 * @property integer $ngay_sua
 * @property string $trang_thai
 *
 * @property PnKhoDetail[] $PnKhoDetails
 */
class PnKho extends \yii\db\ActiveRecord
{
	public function behaviors(){
		return [		  
		  [
				'class' => 'mdm\autonumber\Behavior',
				'attribute' => 'so_ctu', // required
				'group' => Yii::$app->user->identity->id.'NK'.date('m-Y'),
				'value' => '?' , 
				'digit' => 5 // 
		  ],
		];
	}
	
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'pn_kho';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['so_ctu', 'ma_ctu', 'dien_giai', 'ky_hieu_hdon', 'so_hdon', 'ngay_hdon'], 'required'],
            [['uid','ncc_id', 'phong_ban_id', 'kho_id', 'vi_tri_kho_id', 'pptt_id'], 'integer'],
            [['nguoi_nhap', 'nguoi_sua'], 'safe'],
            [['trang_thai'], 'string'],
            [['so_ctu'], 'string', 'max' => 6],            
            [['ma_ctu'], 'string', 'max' => 20],
            [['ten_ncc'], 'string', 'max' => 100],
            [['mst_ncc'], 'string', 'max' => 14],
            [['dien_giai'], 'string', 'max' => 200],
            [['ky_hieu_hdon', 'so_hdon'], 'string', 'max' => 10],
            [['tk_thue'], 'string', 'max' => 5]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'so_ctu' => 'Số CT',
            'ma_ctu' => 'Mã CT',
            'ncc_id' => 'Nhà cung cấp',
            'ten_ncc' => 'Tên Nhà cung cấp',
            'mst_ncc' => 'MST Nhà cung cấp',
            'phong_ban_id' => 'Phòng/Bộ phận',
            'kho_id' => 'Kho',
            'vi_tri_kho_id' => 'Vị trí kho',
            'dien_giai' => 'Nội dung/Diễn giải',
            'ky_hieu_hdon' => 'Ký hiệu HĐ',
            'so_hdon' => 'Số Hóa đơn',
            'ngay_hdon' => 'Ngày HĐ',
            'pptt_id' => 'PP tính thuế',
            'tien_hang' => 'Tiền hàng',
            'tien_thue' => 'Tiền thuế',
            'tien_thanh_toan' => 'Tiền thanh toán',
            'tk_thue' => 'TK thuế',
            'nguoi_nhap' => 'Nguoi Nhap',
            'ngay_nhap' => 'Ngay Nhap',
            'nguoi_sua' => 'Nguoi Sua',
            'ngay_sua' => 'Ngay Sua',
            'trang_thai' => 'Trang Thai',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
   public function getPnKhoDetails(){
      return $this->hasMany(PnKhoDetail::className(), ['pid' => 'id']);
   }
	
	/*
	*	get danh sach nha cung cap
	*/	
	public static function getDsNCC(){
		$model = \frontend\models\DmNhaCungCap::find()->orderBy('ten')->asArray()->all();
		$objs = [];
		foreach($model as $obj)
			$objs[$obj['id']]	= $obj['ma'] . ' - ' . $obj['ten'];
		return $objs;	
		// return ArrayHelper::map($model, 'id', 'ma');
	}
	
	/*
	*	
	*/
	public function getDetails(){		
		return PnKhoDetail::find()->where(['pid'	=>	$this->id])->orderBy('order')->all();
	}
	
	public function getDmHangHoaByNhom(){
		$data = [];
		foreach(DmHangHoa::getDmHangHoaByNhom() as $obj)
			$data[$obj['ma_nhom'].' - '.$obj['ten_nhom']][$obj['ma']] = $obj['ma'].' - '.$obj['ten'];
		return $data;
		echo '<pre>';print_r($data);die;		
	}
	
	public function getDmThueSuat(){
		$data = [];
		foreach(DmThueSuat::getByType('GTGT') as $obj)
			$data[$obj['ty_le']] = $obj['ty_le'];
		return $data;
	}
	
	public function getTkNoList(){
		$model = \frontend\models\Options::find()->where(['name'=>'nhap_kho_tk_no'])->one();		
		$data = unserialize($model->value);
		$obj = [];	
		if($data && !empty($data))
			$obj = explode(';',$data);
		$data = [];
		foreach($obj as $value)
			$data[$value] = $value;
		return $data;
	}
	
	public function getTkCoList(){
		$model = \frontend\models\Options::find()->where(['name'=>'nhap_kho_tk_co'])->one();		
		$data = unserialize($model->value);
		$obj = [];	
		if($data && !empty($data))
			$obj = explode(';',$data);
		$data = [];
		foreach($obj as $value)
			$data[$value] = $value;
		return $data;
	}
}
